/*********************************************************************NVMH3****
Shader that will wobble a object

******************************************************************************/

uniform vec3  LightPosition;
uniform vec3  SurfaceColor;

uniform float time;
uniform float waveFreq;
uniform float waveAmp;
uniform vec4 speed;

varying mat3 rotMatrix; //<- Need this variable to claim buffer WHY ?? 
varying vec4  Color;

// wave functions
struct Wave {
  float freq;  // 2*PI / wavelength
  float amp;   // amplitude
  float phase; // speed * 2*PI / wavelength
  vec3 dir;
};

	
void main()
{
	// add some noise to the position of the vertex
	vec3 normal = gl_Normal;
	vec4 P = gl_Vertex;
	
	#define NWAVES 2

	Wave wave[NWAVES];

	wave[1] = Wave( 3.0 * waveFreq, 0.33 * waveAmp, 1.7, vec3(1, 0, 0) );
	
	// sum waves
	float angle;

	// wave synthesis using two sine waves at different frequencies and phase shift
	wave[0] = Wave( waveFreq, waveAmp, 0.5, vec3(0.0, 1.0, 0.0));
	angle = dot(vec3(normal.x,normal.y,0) , P.xyz) * (wave[0].freq + time  * speed.x ) * wave[0].phase; 	

	P.x += wave[0].amp * sin( angle );		
	P.y += wave[0].amp  * sin( angle);
	
	angle = dot(vec3(normal.x,0,normal.z) , P.xyz) * (wave[0].freq + time  * speed.x ) * wave[0].phase + 0.707; 	
	P.z += wave[0].amp  * sin( angle);
	

	rotMatrix = mat3(normal, normal, normal);
	//compute light for the new gl_Position
	normal = normalize(gl_NormalMatrix * normal);
	vec3 position = vec3(gl_ModelViewMatrix *P);
	vec3 lightVec = normalize(LightPosition - position);
	float diffuse = max(dot(lightVec, normal), 0.0);
	
	if (diffuse <0.125){	
		diffuse = 0.125;}
		
	Color = vec4(SurfaceColor * diffuse, 1.0);
	gl_Position = gl_ModelViewProjectionMatrix * P;
		
}
